package cn.waqwb.sms.controller;

import cn.waqwb.sms.service.CacheService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Set;

/**
 * @author JokerQ
 * @date 2020/5/27 - 15:36
 */
@RestController
@RequestMapping("/cache")
public class CacheController {
    private Logger logger = LoggerFactory.getLogger(CacheController.class);

    @Autowired
    private CacheService cacheService;

    @ApiOperation(value = "字符串设置到缓存")//swagger
    @RequestMapping("/save2Cache")
    public void saveToCache(@ApiParam(name = "key",value = "cache key",required = true) @RequestParam("key") String key,
                            @ApiParam(name = "value",value = "cache value",required = true) @RequestParam("value") String value,
                            @ApiParam(name = "expireSecond",value = "cache expireSecond",required = true) @RequestParam("expireSecond") Integer expireSecond){
        logger.info("key:{},value:{},expireSecond:{}",key,value,expireSecond);
        cacheService.set(key,value,expireSecond);

    }

    @ApiOperation(value = "字符串设置到缓存")//swagger
    @RequestMapping("/saveCache")
    public void saveCache(@ApiParam(name = "key",value = "cache key",required = true) @RequestParam("key") String key,
                            @ApiParam(name = "value",value = "cache value",required = true) @RequestParam("value") String value){
        logger.info("key:{},value:{}",key,value);
        cacheService.set(key,value);

    }

    @ApiOperation(value = "取缓存中的值")
    @RequestMapping("/get")
    public String getStr(@ApiParam(name = "key",value = "cache key",required = true) @RequestParam("key") String key){
        logger.info("key:{}",key);
        return cacheService.get(key);

    }

    @ApiOperation(value = "取缓存中的对象")
    @RequestMapping("/getObject")
    public Object getObject(@ApiParam(name = "key",value = "cache key",required = true) @RequestParam("key") String key){
        logger.info("key:{}",key);
        return cacheService.getObject(key);

    }


    @ApiOperation(value = "删除缓存中的对象")
    @RequestMapping("/del")
    public boolean del(@ApiParam(name = "key",value = "cache key",required = true) @RequestParam("key") String key){
        logger.info("key:{}",key);
        cacheService.del(key);
        return true;
    }

    @ApiOperation(value = "缓存中存储map对象")
    @RequestMapping("/hmset")
    public boolean hmset(@ApiParam(name = "key",value = "cache key",required = true) @RequestParam("key") String key,
                         @ApiParam(name = "map",value = "map",required = true) @RequestBody Map<String, Object> map){
        logger.info("key:{},map:{}",key,map);
        return cacheService.hmset(key,map);
    }

    @ApiOperation(value = "缓存中获取map对象")
    @RequestMapping("/hmet")
    public Map hmget(@ApiParam(name = "key",value = "cache key",required = true) @RequestParam("key") String key){
        logger.info("key:{}",key);
        return cacheService.hmget(key);
    }

    @ApiOperation(value = "缓存中自增")
    @RequestMapping("/incr")
    public Long incr(@ApiParam(name = "key",value = "cache key",required = true) @RequestParam("key") String key,
                     @ApiParam(name = "value",value = "cache value",required = true) @RequestParam("value") long value
                     ){
        logger.info("key:{}",key);
        return cacheService.incr(key,value);
    }

    @ApiOperation(value = "缓存中递减")
    @RequestMapping("/decr")
    public Long decr(@ApiParam(name = "key",value = "cache key",required = true) @RequestParam("key") String key,
                     @ApiParam(name = "value",value = "cache value",required = true) @RequestParam("value") long value
    ){
        logger.info("key:{}",key);
        return cacheService.decr(key,value);
    }

    @ApiOperation(value = "单独设置过期时间")
    @RequestMapping("/expire")
    public boolean expire(@ApiParam(name = "key",value = "cache key",required = true) @RequestParam("key") String key,
                          @ApiParam(name = "expireSecond",value = "cache expireSecond",required = true) @RequestParam("expireSecond") Integer expireSecond){
        logger.info("key:{}",key);
        return cacheService.expire(key,expireSecond);
    }

    @ApiOperation(value = "取keys集合")
    @RequestMapping("/keys")
    public Set<String> keys(@ApiParam(name = "pattern",value = "cache pattern",required = true) @RequestParam("pattern") String pattern){
        logger.info("pattern:{}",pattern);
        return cacheService.keys(pattern);
    }
}
